package com.aier.school.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.common.TeacherLeaveType;
import com.aier.school.entity.SchoolGrade;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.entity.TeacherAttendanceMonth;
import com.aier.school.entity.TeacherIcardInfo;
import com.aier.school.entity.TeacherLeave;
import com.aier.school.entity.UserIcardRecord;
import com.aier.school.service.ISchoolIcCardService;
import com.aier.school.service.ISchoolLeaveService;
import com.aier.school.service.ISchoolMonAttendService;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;

@Scope("prototype")
@Component("schoolMonthAttendAction")
public class SchoolMonthAttendAction extends SchoolBaseAction {

    /**
     * 学校管理-管理员查看的每月考勤 与 教师查看的每月考勤
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name = "schoolService")
    private ISchoolService schoolService;
    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;
    
    @Resource(name = "schoolMonAttendService")
    private ISchoolMonAttendService schoolMonAttendService;
    
    @Resource(name = "schoolLeaveService")
    private ISchoolLeaveService schoolLeaveService;
    
    @Resource(name = "schoolIcCardService")
    private ISchoolIcCardService schoolIcCardService;    
    
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
        
    private Integer sid;                        //学校id
    private Long tid;                            //教师id
    private short isadmin_school;                //是否学校管理员
    
    private List<String> tnameAccountList;        //教师名字账号列表
    private List<SchoolGrade> schoolGradeList;    //职务列表    
    private List<SchoolTeacher> schoolTeacherList;        //学校教师列表    
    private List<TeacherAttendanceMonth> attMonths;        //每月考勤
    private List<TeacherIcardInfo> attDetails;            //每日打卡明细
    private List<TeacherLeave> terLeaves;                //请假异常信息
    
    private Long gid;            //职务id
    private String account;        //教师账号
    private String monthBeginValue;                //开始年月
    private String monthEndValue;                //结束年月
    private String dayEndValue;                    //开始时间
    private String dayBeginValue;                //结束时间
    private SchoolTeacher schoolTeacher;        //学校教师
    
    private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数
    
    private String month;   // 教师考勤月份
    private List<UserIcardRecord> teaIcardInfList;//进出园记录列表
    
    @Override
    public String execute() {
        tnameAccountList = schoolTeacherService.searchTNameAndAccountBySid(sid);
        tnameAccountList.add(0, "所有教师");
        schoolGradeList = schoolService.searchSchoolGradeList(sid);
        
        return SUCCESS;
    }
    
    //查询每月考勤信息  学校管理员
    @SuppressWarnings("unchecked")
    public String searchATMList_admin(){
        s = 0;
        Integer year_begin = null,month_begin = null,year_end = null,month_end = null;
        if(page<=0){
            page=1;
        }
        if (monthBeginValue != null && !monthBeginValue.equals("")) {
            String[] st = monthBeginValue.split("-");
            year_begin = Integer.parseInt(st[0]);
            month_begin = Integer.parseInt(st[1]);
        }
        if (monthEndValue != null && !monthEndValue.equals("")) {
            String[] st = monthEndValue.split("-");
            year_end = Integer.parseInt(st[0]);
            month_end = Integer.parseInt(st[1]);
        }
        List<Long> tids = new ArrayList<Long>();
        List<SchoolTeacher> teachers = schoolTeacherService.selectTeacher(sid,account,gid);
        if(teachers!=null&&teachers.size()!=0){
            for(SchoolTeacher teacher:teachers){
                tids.add(teacher.getTid());
            }
            Map<String,Object> map = schoolMonAttendService.searchAttMonthsByCondition(page,SchoolFinalPageSize.MONTH_ATTEND_LIST_PAGESIZE,
                    sid,tids,year_begin,month_begin,year_end,month_end);
            if(null!=map){
                attMonths = (List<TeacherAttendanceMonth>) map.get(ICommonDAO.OBJ_LIST);
                if (null != attMonths) {
                    s = 1;
                }
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);        
            }        
        }
        return SUCCESS;
    }
    
    //查询打卡信息 学校管理员
    @SuppressWarnings("unchecked")
    public String searchADList_admin(){
        s = 0;
        if(page<=0){
            page=1;
        }
        List<Long> tids = new ArrayList<Long>();    
        List<SchoolTeacher> teachers = schoolTeacherService.selectTeacher(sid,account,gid);
        if(teachers!=null&&teachers.size()!=0){
            for(SchoolTeacher teacher:teachers){
                tids.add(teacher.getTid());
            }
            Map<String,Object> map = schoolMonAttendService.searchAttDetailsByCondition(page,
                    SchoolFinalPageSize.ATTEND_DETAIL_LIST_PAGESIZE,
                    sid,tids,dayBeginValue,dayEndValue);
            if(null!=map){
                attDetails = (List<TeacherIcardInfo>) map.get(ICommonDAO.OBJ_LIST);
                if (null != attDetails) {
                    s = 1;
                }
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
            }
        }
        return SUCCESS;
    }
    
    //查询异常请假信息 学校管理员
    @SuppressWarnings("unchecked")
    public String searchLeaveList_admin(){
        s = 0;
        if(page<=0){
            page=1;
        }
        List<Long> tids = new ArrayList<Long>();    
        List<SchoolTeacher> teachers = schoolTeacherService.selectTeacher(sid,account,gid);
        if(teachers!=null&&teachers.size()!=0){
            for(SchoolTeacher teacher:teachers){
                tids.add(teacher.getTid());
            }
            Map<String,Object> map_leave = schoolLeaveService.searchTerLeavesByCondition(page,
                    SchoolFinalPageSize.LEAVE_EXCEPTION_LIST_PAGESIZE,
                    sid,tids,TeacherLeaveType.TYPE_HABDLE_BJ,dayBeginValue,dayEndValue);
            if(null!=map_leave){
                terLeaves = (List<TeacherLeave>) map_leave.get(ICommonDAO.OBJ_LIST);        
                page = (Integer) map_leave.get(ICommonDAO.PAGE);
                page_size = (Integer) map_leave.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map_leave.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map_leave.get(ICommonDAO.COUNT);        
            }
        }

        return SUCCESS;
    }

//====================================================================================================
    
    //查询每月考勤信息  学校普通教师
    @SuppressWarnings("unchecked")
    public String searchATMList_normal(){
        s = 0;
        Integer year_begin = null,month_begin = null,year_end = null,month_end = null;
        if(page<=0){
            page=1;
        }
        if (monthBeginValue != null && !monthBeginValue.equals("")) {
            String[] st = monthBeginValue.split("-");
            year_begin = Integer.parseInt(st[0]);
            month_begin = Integer.parseInt(st[1]);
        }
        if (monthEndValue != null && !monthEndValue.equals("")) {
            String[] st = monthEndValue.split("-");
            year_end = Integer.parseInt(st[0]);
            month_end = Integer.parseInt(st[1]);
        }
        List<Long> tids = new ArrayList<Long>();
        tids.add(tid);
        Map<String,Object> map = schoolMonAttendService.searchAttMonthsByCondition(page,SchoolFinalPageSize.MONTH_ATTEND_LIST_PAGESIZE,
                sid,tids,year_begin,month_begin,year_end,month_end);
        if(null!=map){
            attMonths = (List<TeacherAttendanceMonth>) map.get(ICommonDAO.OBJ_LIST);
            if (null != attMonths) {
                s = 1;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);        
        }        
        return SUCCESS;
    }
    
    //查询打卡信息  学校普通教师
    @SuppressWarnings("unchecked")
    public String searchADList_normal(){
        s = 0;
        if(page<=0){
            page=1;
        }
        List<Long> tids = new ArrayList<Long>();    
        tids.add(tid);
        Map<String,Object> map = schoolMonAttendService.searchAttDetailsByCondition(page,SchoolFinalPageSize.ATTEND_DETAIL_LIST_PAGESIZE,
                sid,tids,dayBeginValue,dayEndValue);
        if(null!=map){
            attDetails = (List<TeacherIcardInfo>) map.get(ICommonDAO.OBJ_LIST);
            if (null != attDetails) {
                s = 1;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
        }
        return SUCCESS;
    }
    
    //查询异常请假信息  学校普通教师
    @SuppressWarnings("unchecked")
    public String searchLeaveList_normal(){
        s = 0;
        if(page<=0){
            page=1;
        }
        List<Long> tids = new ArrayList<Long>();    
        tids.add(tid);
        Map<String,Object> map_leave = schoolLeaveService.searchTerLeavesByCondition(page,SchoolFinalPageSize.LEAVE_EXCEPTION_LIST_PAGESIZE,
                sid,tids,TeacherLeaveType.TYPE_HABDLE_BJ,dayBeginValue,dayEndValue);
        if(null!=map_leave){
            terLeaves = (List<TeacherLeave>) map_leave.get(ICommonDAO.OBJ_LIST);        
            page = (Integer) map_leave.get(ICommonDAO.PAGE);
            page_size = (Integer) map_leave.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map_leave.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map_leave.get(ICommonDAO.COUNT);        
        }
        return SUCCESS;
    }
    
    /**
     * 教师打卡记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchTIIList(){
        s = 0;
        if(page<=0){
            page=1;
        }
        Map<String,Object> map= schoolIcCardService.searchTeaIcardInfoListByMonthTid(page, SchoolFinalPageSize.ATTEND_DETAIL_LIST_PAGESIZE, 
                    month,tid);
        
        if(null!=map){
            teaIcardInfList = (List<UserIcardRecord>) map.get(ICommonDAO.OBJ_LIST);
            if (null != teaIcardInfList) {
                s = 1;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            
        }
        return SUCCESS;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public short getIsadmin_school() {
        return isadmin_school;
    }

    public void setIsadmin_school(short isadmin_school) {
        this.isadmin_school = isadmin_school;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<String> getTnameAccountList() {
        return tnameAccountList;
    }

    public List<SchoolGrade> getSchoolGradeList() {
        return schoolGradeList;
    }

    public List<SchoolTeacher> getSchoolTeacherList() {
        return schoolTeacherList;
    }

    public List<TeacherAttendanceMonth> getAttMonths() {
        return attMonths;
    }

    public List<TeacherIcardInfo> getAttDetails() {
        return attDetails;
    }

    public List<TeacherLeave> getTerLeaves() {
        return terLeaves;
    }

    public SchoolTeacher getSchoolTeacher() {
        return schoolTeacher;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setSchoolMonAttendService(
            ISchoolMonAttendService schoolMonAttendService) {
        this.schoolMonAttendService = schoolMonAttendService;
    }

    public void setSchoolLeaveService(ISchoolLeaveService schoolLeaveService) {
        this.schoolLeaveService = schoolLeaveService;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setMonthBeginValue(String monthBeginValue) {
        this.monthBeginValue = monthBeginValue;
    }

    public void setMonthEndValue(String monthEndValue) {
        this.monthEndValue = monthEndValue;
    }

    public void setDayEndValue(String dayEndValue) {
        this.dayEndValue = dayEndValue;
    }

    public void setDayBeginValue(String dayBeginValue) {
        this.dayBeginValue = dayBeginValue;
    }

    public void setSchoolIcCardService(ISchoolIcCardService schoolIcCardService) {
        this.schoolIcCardService = schoolIcCardService;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public List<UserIcardRecord> getTeaIcardInfList() {
        return teaIcardInfList;
    }

    
    
    
    
}
